import Vue from "vue"
let prototype = Vue.prototype;

export default {
    /**
     * 获取列表
     */
    getList(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    let list = [];
                    if (options.listType == "t1" || options.listType == "t2") {
                        response.data.forEach(item => {
                            list.push({
                                listType: options.listType,
                                id: item.id,
                                orderId: item.order,
                                cate: item.cate_name,
                                content: item.desc,
                                createTime: item.time,
                                serviceTime: item.times,
                                orderType: item.types,
                                state: item.status,
                                price: item.money,
                            });
                        });
                    } else if (options.listType == "t3") {
                        response.data.forEach(item => {
                            list.push({
                                listType: options.listType,
                                id: item.id,
                                orderId: item.order,
                                cate: "配送订单",
                                content: "",
                                createTime: item.times,
                                serviceTime: item.starttime,
                                orderType: 1,
                                state: item.status,
                                price: item.money,
                            });
                        });
                    }

                    return resolve(list);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 获取详情
     */
    getDetail(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    let order = {};
                    if (options.listType == "t1" || options.listType == "t2") {
                        order = {
                            listType: options.listType,
                            id: response.data.id,
                            orderId: response.data.order,
                            cate: response.data.cate_name,
                            content: response.data.desc,
                            createTime: response.data.time,
                            serviceTime: response.data.times,
                            orderType: response.data.types,
                            state: response.data.status,
                            price: response.data.money,
                            orderId: response.data.order,
                            address: {
                                address: response.data.address.address,
                                detail: response.data.address.doorplate,
                                name: response.data.address.name,
                                gender: response.data.address.sex == "男" ? 1 : 2,
                                mobile: response.data.address.mobil,
                                lng: response.data.address.lng,
                                lat: response.data.address.lat,
                            },
                        };
                        if (options.listType == "t1") {
                            order.images = response.data.goods.map((item) => item.thumbnail);
                        } else {
                            order.images = response.data.img;
                        }
                    } else if (options.listType == "t3") {
                        order = {
                            listType: options.listType,
                            id: response.data.id,
                            orderId: response.data.order,
                            cate: "配送订单",
                            createTime: response.data.times,
                            serviceTime: response.data.starttime,
                            pickupAddress: {
                                address: response.data.address_a.address,
                                detail: response.data.address_a.doorplate,
                                name: response.data.address_a.name,
                                gender: response.data.address_a.sex == "男" ? 1 : 2,
                                mobile: response.data.address_a.mobil,
                                lng: response.data.address_a.lng,
                                lat: response.data.address_a.lat,
                            },
                            unloadAddress: {
                                address: response.data.address_b.address,
                                detail: response.data.address_b.doorplate,
                                name: response.data.address_b.name,
                                gender: response.data.address_b.sex == "男" ? 1 : 2,
                                mobile: response.data.address_b.mobil,
                                lng: response.data.address_b.lng,
                                lat: response.data.address_b.lat,
                            },
                            state: response.data.status,
                            price: response.data.money,
                        };
                    }
                    return resolve(order);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 接单
     */
    getOrder(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options).then(response => {
                if (response.code == 1) {
                    return resolve(response.msg);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    }
}
